package com.android.job.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.android.job.model.MyLocation;
import com.android.job.model.MyInfo;

/**
 * 表单操作逻辑层
 * @author Administrator
 *
 */
public class FormDBHelper
{
	/**
	* DBHelper dbhelper操作数据库相关
	*/
	private DBHelper dbhelper = null;

	public FormDBHelper(final Context context)
	{
		if (dbhelper == null)
		{
			dbhelper = new DBHelper(context);
		}
	}
	
   
	/**
	 * 关闭数据库 若dbhlper为空则将其关闭
	 */
	public void closeDB()
	{
		if (dbhelper != null)
		{
			dbhelper.close();
		}
	}

	/**
	 * 插入数据
	 */
	public void insertForm(MyInfo request)
	{
		ContentValues values = new ContentValues();
		values.put("workId", request.workId);
		values.put("name", request.name);
		values.put("dept", request.dept);
		values.put("type", request.type);
		values.put("begin", request.startTime);
		values.put("end", request.endTime);
		values.put("days", request.days);
		values.put("reason", request.reason);
		values.put("approve", request.approve);
		try
		{
			dbhelper.insert("table_request", values);
		}
		catch (Exception e)
		{

		}
	}
	
	/**
	 * 插入签到位置信息
	 * @param location
	 */
	public void intertLocation(MyLocation location)
	{
		ContentValues values = new ContentValues();
//		values.put("name", location.name);
		values.put("location", location.location);
		values.put("time", location.time);
		try
		{
			dbhelper.insert("my_location", values);
		}
		catch (Exception e)
		{
			
		}
	}
	
	/**
	 * 查询所有的签到位置
	 * @return
	 */
	public ArrayList<MyLocation> getAllLocation()
	{
		ArrayList<MyLocation> locations = new ArrayList<MyLocation>();
		Cursor cursor = dbhelper.query();
		try
		{
			if(null!=cursor)
			{
				if(cursor.moveToFirst())
				{
					do
					{
						MyLocation location = new MyLocation();
						location.id = cursor.getInt(cursor.getColumnIndex("id"));
//						location.name =cursor.getString(cursor.getColumnIndex("name"));
						location.time =cursor.getString(cursor.getColumnIndex("time"));
						location.location =cursor.getString(cursor.getColumnIndex("location"));

						locations.add(location);
					}
					while (cursor.moveToNext());
				}
			}
		}
		catch (Exception e)
		{
		}finally
		{
			if(null!=cursor)
			{
			cursor.close();
			}
		}
		return locations;
	}
	
	

	/**
	 * 查询数据
	 * @param request
	 * @return
	 */
	public Cursor queryForm(MyInfo request)
	{
		Cursor cursor = dbhelper.query(request.workId, request.approve);

		return cursor;
	}

	public Cursor queryForm(int approve)
	{
		Cursor cursor = dbhelper.query(approve);

		return cursor;
	}

	public int update(MyInfo request,int state)
	{
		int count = dbhelper.update(request.id,state);
		return count;
	}
}
